﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint.Administration;

namespace Diplo.Twitter
{
    /// <summary>
    /// Quick and dirty implementation of IOAuthData that loads and saves data into an example XML file
    /// </summary>
    /// <remarks>
    /// For example only - don't use in production without making much more robust!
    /// </remarks>
    public class OAuthUserProfileData : IOAuthData
    {
        public string AccountName { get; set; }

        public string ScreenName { get; set; }

        public string Token { get; set; }

        public string TokenSecret { get; set; }

        public string UserID { get; set; }

        public bool IsAuthenticated { get; set; }

        public OAuthUserProfileData()
        {
        }

        /// <summary>
        /// Loads the data from the User Profile file
        /// </summary>
        /// <param name="accountname">Login name of the account to get the twitter settings form.</param>
        public OAuthUserProfileData(string accountname)
        {
            this.AccountName = accountname;

            SPServiceContext context = SPServiceContext.GetContext(SPServiceApplicationProxyGroup.Default, SPSiteSubscriptionIdentifier.Default);
            UserProfileManager upm = new UserProfileManager(context, true);
            UserProfile userprofile = upm.GetUserProfile(accountname);

            if (userprofile["TwitterScreenName"].Value != null)
                this.ScreenName = userprofile["TwitterScreenName"].Value.ToString();
            if (userprofile["TwitterToken"].Value != null)
                this.Token = userprofile["TwitterToken"].Value.ToString();
            if (userprofile["TwitterTokenSecret"].Value != null)
                this.TokenSecret = userprofile["TwitterTokenSecret"].Value.ToString();
            if (userprofile["TwitterUserID"].Value != null)
                this.UserID = userprofile["TwitterUserID"].Value.ToString();

            if (userprofile["TwitterScreenName"].Value != null &&
                userprofile["TwitterToken"].Value != null &&
                userprofile["TwitterTokenSecret"].Value != null &&
                userprofile["TwitterUserID"].Value != null)
            {
                IsAuthenticated = true;
            }
            else
            {
                IsAuthenticated = false;
            }
        }

        /// <summary>
        /// Saves the data back to the XML file
        /// </summary>
        public void Save()
        {
            SPServiceContext context = SPServiceContext.GetContext(SPServiceApplicationProxyGroup.Default, SPSiteSubscriptionIdentifier.Default);
            UserProfileManager upm = new UserProfileManager(context, true);
            UserProfile userprofile = upm.GetUserProfile(this.AccountName);

            userprofile["TwitterToken"].Value = this.Token;
            userprofile["TwitterTokenSecret"].Value = this.TokenSecret;
            userprofile["TwitterUserID"].Value = this.UserID.ToString();
            userprofile["TwitterScreenName"].Value = this.ScreenName;

            userprofile.Commit();
        }
    }
}
